import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'login', component: () => import('@/views/login') },
  {
    path: '/',

    component: () => import('@/views/layout'),
    // redirect: '/dashboard',
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('@/views/dashboard')
      },
      {
        path: '/articles/list',
        name: '/articles/list',
        component: () => import('@/views/articlesList/index')
      },
      {
        path: '/questions/randoms',
        name: 'questions-randoms',
        component: () => import('@/views/questions/questions-randoms')
      },

      // 学科
      {
        path: '/subjects',
        component: () => import('@/views/subject/Subject.vue')
      },
      // 目录
      {
        path: '/directorys',
        component: () => import('@/views/subject/directorys.vue')
      },
      // 标签
      {
        path: '/tags',
        component: () => import('@/views/subject/tags.vue')
      },
      {
        path: '/randoms',
        component: () => import('@/views/questions/randoms.vue')
      },
      // 企业管理路由
      {
        path: '/companys',
        component: () => import('@/views/companys/companys.vue')
      },
      {
        path: '/base/users',
        name: 'user',
        component: () => import('@/views/backStageManagement/user')
      },
      {
        path: '/base/menus',
        name: 'menu',
        component: () => import('@/views/backStageManagement/menu')
      },
      {
        path: '/base/permissions',
        name: 'permissions',
        component: () => import('@/views/backStageManagement/permissions')
      },
      // 题库管理

      {
        // 基础题库
        path: '/question/basics',
        name: 'question_basics',
        component: () => import('@/views/question/basics/basics-question')
      },
      {
        // 精选题库
        path: '/question/choiceness',
        name: 'question_choiceness',
        component: () =>
          import('@/views/question/choiceness/choiceness-question')
      },
      {
        // 试题录入
        path: '/question/entering',
        name: 'question_entering',
        component: () => import('@/views/question/entering/entering-question')
      },
      {
        // 组题列表
        path: '/question/list',
        name: 'question_list',
        component: () => import('@/views/question/list/list-question')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫,控制访问权限
router.beforeEach((to, from, next) => {
  // 如果用户访问登录页，直接放行，next
  if (to.path === '/login') {
    return next()
  }
  // 用户不是访问登录页，获取浏览器sessionStroage中的token
  // 存在的的话，放行，
  // 不存在的话，跳转至登录页
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    // 不存在
    return next('/login')
  } else {
    // 存在
    return next()
  }
})
/**
 * 修改首页面路由重复点击报错
 */
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}
export default router
